# -*- coding: utf-8-unix; fill-column: 95; -*-

###############################################################################################
#
# This file is part of Escapade Trip Planner, a social web application for creating and
# sharing motorcycle roadmaps.
#
# Copyright © 2011, Pierre-François Gomez <pef.gomez@gmail.com>
#
# Escapade Trip Planner is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
#
# Escapade Trip Planner is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with Escapade Trip
# Planner. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################################

import shelve
import uuid

import config

class RoadmapBackend:
    def __init__(self):
        self.store = shelve.open(config.shelve_backend['store_dir'] + "/roadmap_store")

    def create(self, p_roadmap):
        l_id = str(uuid.uuid1())
        if l_id in self.store:
            raise(KeyError)
        p_roadmap['id'] = l_id
        self.store[l_id] = p_roadmap
        return(self.store[l_id])

    def read(self):
        return(self.store.keys())

    def update(self, p_roadmap):
        if p_roadmap['id'] in self.store:
            self.store[p_roadmap['id']] = p_roadmap
            return(self.store[p_roadmap['id']])
        else:
            raise(KeyError)

    def delete(self, p_id):
        del(self.store[p_id])

    def item(self, p_id):
        return(self.store[p_id])

    def contains(self, p_id):
        return(p_id in self.store)
